package com.wl156.activities;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.widget.ProviceSelector;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;

public class MyOrder extends Activity implements OnClickListener {

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	private ProviceSelector mFrom;
	private ProviceSelector mTo;
	private EditText mCargosDescription;
	private EditText mDriverName;
	private EditText mPlateNumber;
	private EditText mDriverTel;
	private EditText mMerchandiser;
	private EditText mMerchandiserTel;
	private EditText mTonnage;
	private EditText mAcceptDateStart;
	private EditText mAcceptDateEnd;

	private Button mConfirm;
	private ToggleButton mMode;

	protected int mCurrentPageIndex = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_my_order);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mFrom = (ProviceSelector) findViewById(R.id.provice_selector);
		mTo = (ProviceSelector) findViewById(R.id.to_provice_selector);
		mCargosDescription = (EditText) findViewById(R.id.CargosDescription);
		mDriverName = (EditText) findViewById(R.id.driverName);
		mPlateNumber = (EditText) findViewById(R.id.plateNumber);
		mDriverName = (EditText) findViewById(R.id.driverName);
		mMerchandiser = (EditText) findViewById(R.id.Merchandiser);
		mMerchandiserTel = (EditText) findViewById(R.id.MerchandiserTel);
		mTonnage = (EditText) findViewById(R.id.tonnage);
		mAcceptDateStart = (EditText) findViewById(R.id.acceptDateStart);
		mAcceptDateEnd = (EditText) findViewById(R.id.acceptDateEnd);

		mConfirm = (Button) findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		mMode = (ToggleButton) findViewById(R.id.mode);
		mMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm: {
			try {
				mWebServiceClient.companyGetMyOrders(mCertificate, mFrom
						.getProvice(), mFrom.getCity(), mFrom.getRegion(), mTo
						.getProvice(), mTo.getCity(), mTo.getRegion(), "",
						mCargosDescription.getText().toString(), mDriverName
								.getText().toString(), mPlateNumber.getText()
								.toString(), mDriverTel.getText().toString(),
						mMerchandiser.getText().toString(), mMerchandiserTel
								.getText().toString(), mTonnage.getText()
								.toString(), "", mAcceptDateStart.getText()
								.toString(), mAcceptDateEnd.getText()
								.toString(), mCurrentPageIndex, 10, null);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}
		}
			break;
		}
	}
}
